/*
 *
 */

package com.hlingsoft.kkh.action;

import com.hlingsoft.kkh.constant.ApplicationConstant;
import com.hlingsoft.kkh.dao.*;
import com.hlingsoft.kkh.entity.*;

import com.hlingsoft.kkh.service.AdminPushService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * 职位
 *
 * @author Herbert
 */
@Controller
@RequestMapping("/job")

public class JobAction extends BaseAction {

    @Autowired
    private AdminJobDao jobDao;
    @Autowired
    private MerchantDao merchantDao;
    @Autowired
    private AdminApplicationDao applicationDao;
    @Autowired
    private AdminResumeDao resumeDao;

    @Autowired
    private MessageDao messageDao;




    private static final int rows = 10;

    /**
     * 职位列表
     * @param categoryId
     * @param pageNum
     * @param request
     * @return
     */
    @RequestMapping(value = "/list.json", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> list(
            @RequestParam(value = "categoryId") long categoryId,
            @RequestParam(value = "pageNum", defaultValue = "0") int pageNum,
            HttpServletRequest request) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        try {
            List<Job> jobList = jobDao.getAllJobListByCategory(categoryId, pageNum * rows, rows);
            resultMap.put("jobList", jobList);
            resultMap.put("result", true);

        } catch (Exception e) {
            e.printStackTrace();
            resultMap.put("result", false);
        }
        return resultMap;
    }

    /**
     * 摇一摇
     * @param request
     * @return
     */
    @RequestMapping(value = "/yaoyiyao.json", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> yaoyiyao(
            HttpServletRequest request) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        try {
            List<Job> jobList = jobDao.getAllJobList(0, 10);
            //随机获取一个job
            Random r = new Random();
            int n5 = r.nextInt(10);
            Job job=jobList.get(n5)    ;
            resultMap.put("job", job);
            resultMap.put("result", true);

        } catch (Exception e) {
            e.printStackTrace();
            resultMap.put("result", false);
        }
        return resultMap;
    }

    @RequestMapping(value = "/fujin.json", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> fujin(
            HttpServletRequest request) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        try {
            List<Job> jobList = jobDao.getAllJobList( 0, 999);
            resultMap.put("jobList", jobList);
            resultMap.put("result", true);

        } catch (Exception e) {
            e.printStackTrace();
            resultMap.put("result", false);
        }
        return resultMap;
    }


    /**
     * 职位详情页
     * @param jobId
     * @param appUserId
     * @param request
     * @return
     */
    @RequestMapping(value = "/detail.json", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> detail(
            @RequestParam(value = "jobId") long jobId,
            @RequestParam(value = "appUserId", defaultValue = "-1") long appUserId,
            HttpServletRequest request) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        try {
            Job job = jobDao.getJobById(jobId);
            MerchantInfo merchant = merchantDao.getMerchantById(job.getMerchantId());
            //如果客户端传值appUserId
            if (appUserId != -1) {
                Resume resume = resumeDao.getDefaultResumeByUserId(appUserId);

                if (resume instanceof Resume) {
                    resultMap.put("defaultResumeId", resume.getId());
                }
            }
            if (merchant instanceof MerchantInfo) {
                resultMap.put("merchant", merchantDao.getMerchantById(job.getMerchantId()));
                resultMap.put("job", job);

                resultMap.put("merchantJobCount", jobDao.getJobListByMerchantCount(merchant.getId()));
                resultMap.put("result", true);
            } else {
                resultMap.put("result", false);
                resultMap.put("message", "商户信息已被删除");
            }

        } catch (Exception e) {
            e.printStackTrace();
            resultMap.put("result", false);
        }

        return resultMap;

    }

    /**
     * 商户职位列表
     * @param merchantId
     * @param request
     * @return
     */
    @RequestMapping(value = "/merchantJobList.json", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> merchantJobList(
            @RequestParam(value = "merchantId") long merchantId,
            HttpServletRequest request) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        try {
            resultMap.put("jobList", jobDao.getAllJobListByMerchant(merchantId, 0, 999));
            resultMap.put("result", true);

        } catch (Exception e) {
            e.printStackTrace();
            resultMap.put("result", false);
        }

        return resultMap;

    }


    /**
     * 商户职位列表
     * @param request
     * @return
     */
    @RequestMapping(value = "/search.json", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> search(
            @RequestParam(value = "jobName")String jobName,
            HttpServletRequest request) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        try {
            resultMap.put("jobList",jobDao.getSearchJob("",jobName,"",0,999));
            resultMap.put("result", true);

        } catch (Exception e) {
            e.printStackTrace();
            resultMap.put("result", false);
        }

        return resultMap;

    }



    /**
     * 投递简历
     *
     * @param jobId
     * @param appUserId
     * @param resumeId
     * @param request
     * @return
     */
    @RequestMapping(value = "/application.json", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> application(
            @RequestParam(value = "jobId") long jobId,
            @RequestParam(value = "appUserId") long appUserId,
            @RequestParam(value = "resumeId") long resumeId,
            HttpServletRequest request) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        try {
            final AppUser appUser = userDao.getUserById(appUserId);
            Job job = jobDao.getJobById(jobId);

            //判断同一份简历是否投递了同一个职位，若有提示职位已投递
            ApplicationHistory appApplication = applicationDao.getUserApplicationByJobIdAndResumeId(jobId, resumeId);
            if (appApplication instanceof ApplicationHistory) {
                resultMap.put("result", false);
                resultMap.put("msg", "您已经申请过该职位,请到申请记录中查询进度");
            } else {
                final MerchantInfo merchantInfo = merchantDao.getMerchantById(job.getMerchantId());
                ApplicationHistory applicationHistory = new ApplicationHistory();
                applicationHistory.setCreateTime(new Date());
                applicationHistory.setAppUserId(appUserId);
                applicationHistory.setMerchantId(merchantInfo.getId());
                applicationHistory.setJobId(jobId);
                applicationHistory.setUpdateTime(new Date());
                applicationHistory.setResumeId(resumeId);
                applicationHistory.setIsDelete(0);//未删除
                applicationHistory.setStatus(ApplicationConstant.Status.NEW);
//            adminPushService.pushToClient("您的申请已被雇主接收,请耐心等待..",new ArrayList<String>(){
//                {
//                    add(appUser.getUdid());
//                }
//            }, AdminPushService.JOB,false);
                String alert = appUser.getName() + "正在申请" + job.getName();
                try {
                    adminPushService.pushToClient(alert, new ArrayList<String>() {
                        {
                            add(merchantInfo.getUdId());
                        }
                    }, AdminPushService.JOB, true,true);
                    applicationDao.add(applicationHistory);  //添加记录到用户职位申请记录表(对应表application_history)
//                applicationDao.addMerchantMessage(applicationHistory)
                    applicationDao.addMerchantMessage(applicationHistory); //添加记录到商户职位申请表(对应表merchant_application_history)
                } catch (Exception e) {
                    e.printStackTrace();
                }
                job.setCandidateNum(job.getCandidateNum() + 1);//候选人+1
                jobDao.updateJob(job);

                //给用户发送信息
                AppUserMessage messageVo = new AppUserMessage();
                messageVo.setCreateTime(new Date());
                messageVo.setContent("您申请的" + job.getName() + "已被雇主接收,请耐心等待..");
                messageVo.setStatus(0);
                messageVo.setAppUserId(appUserId);
                messageVo.setExtra(Integer.valueOf(AdminPushService.JOB));
                messageVo.setTitle("申请通知");
                messageDao.addUserMessage(messageVo);
                //给商户发送信息
                MerchantMessage merchantMessage = new MerchantMessage();
                merchantMessage.setContent(alert);
                merchantMessage.setTitle("申请通知");
                merchantMessage.setMerchantId(merchantInfo.getId());
                merchantMessage.setCreateTime(new Date());
                merchantMessage.setStatus(0);
                merchantMessage.setExtra(Integer.valueOf(AdminPushService.JOB));
//            merchantMessage.setStatus();
                messageDao.addMerchantMessage(merchantMessage);
                resultMap.put("result", true);
            }



        } catch (Exception e) {
            e.printStackTrace();
            resultMap.put("result", false);
            resultMap.put("msg", "系统");
        }

        return resultMap;

    }


}
